/*
 * TestUtils.c
 *
 *  Created on: Dec 7, 2012
 *      Author: Larry.Li
 */
#include "../BitBoard.h"
#include <assert.h>

BitBoard emptyBoard() {
	BitBoard board = { .startingBits = 0, .middleBits = 0, .endingBits = 0 };
	return board;
}

void genBitBoardArrayMapping(BitBoard *board, int *array) {
	int i;
	for (i = 0; i < 32; i++) {
		array[i] = (board->startingBits >> i) & 0x1UL;
	}
	for (; i < 64; i++) {
		array[i] = (board->middleBits >> (i - 32)) & 0x1UL;
	}
	for (; i < 90; i++) {
		array[i] = (board->endingBits >> (i - 64)) & 0x1UL;
	}
}

void genAllPossibleBitArrayOfRank(int array[512][9]) {
	int b0, b1, b2, b3, b4, b5, b6, b7, b8, index;
	for (b0 = 0; b0 < 2; b0++) {
		for (b1 = 0; b1 < 2; b1++) {
			for (b2 = 0; b2 < 2; b2++) {
				for (b3 = 0; b3 < 2; b3++) {
					for (b4 = 0; b4 < 2; b4++) {
						for (b5 = 0; b5 < 2; b5++) {
							for (b6 = 0; b6 < 2; b6++) {
								for (b7 = 0; b7 < 2; b7++) {
									for (b8 = 0; b8 < 2; b8++) {
										index = b0 + b1 * 2 + b2 * 4 + b3 * 8
												+ b4 * 16 + b5 * 32 + b6 * 64
												+ b7 * 128 + b8 * 256;
										assert(index < 512);
										array[index][0] = b0;
										array[index][1] = b1;
										array[index][2] = b2;
										array[index][3] = b3;
										array[index][4] = b4;
										array[index][5] = b5;
										array[index][6] = b6;
										array[index][7] = b7;
										array[index][8] = b8;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

void genAllPossibleBitArrayOfFile(int array[1024][10]) {
	int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, index;
	for (b0 = 0; b0 < 2; b0++) {
		for (b1 = 0; b1 < 2; b1++) {
			for (b2 = 0; b2 < 2; b2++) {
				for (b3 = 0; b3 < 2; b3++) {
					for (b4 = 0; b4 < 2; b4++) {
						for (b5 = 0; b5 < 2; b5++) {
							for (b6 = 0; b6 < 2; b6++) {
								for (b7 = 0; b7 < 2; b7++) {
									for (b8 = 0; b8 < 2; b8++) {
										for (b9 = 0; b9 < 2; b9++) {
											index = b0 + b1 * 2 + b2 * 4
													+ b3 * 8 + b4 * 16 + b5 * 32
													+ b6 * 64 + b7 * 128
													+ b8 * 256 + b9 * 512;
											assert(index < 1024);
											array[index][0] = b0;
											array[index][1] = b1;
											array[index][2] = b2;
											array[index][3] = b3;
											array[index][4] = b4;
											array[index][5] = b5;
											array[index][6] = b6;
											array[index][7] = b7;
											array[index][8] = b8;
											array[index][9] = b9;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

void genBitBoardFromBits(BitBoard *board, int rowNum, unsigned long bits) {
	if (rowNum <= 2) {
		board->startingBits = bits << rowNum * 9;
	} else if (rowNum == 3) {
		board->startingBits = bits << 27;
		board->middleBits = bits >> 5;
	} else if (rowNum <= 6) {
		board->middleBits = bits << ((rowNum - 4) * 9 + 4);
	} else if (rowNum == 7) {
		board->middleBits = bits << 31;
		board->endingBits = bits >> 1;
	} else if (rowNum <= 9) {
		board->endingBits = bits << ((rowNum - 8) * 9 + 8);
	}
}

int isMovementExists(Movement movements[194], int movementCount, int from,
		int to) {
	int i;
	for (i = 0; i < movementCount; i++) {
		if (movements[i].from == from && movements[i].to == to) {
			return 1;
		}
	}
	return 0;
}
